package com.cl.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class AuthGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //调用请求之前统计时间
        Long startTime = System.currentTimeMillis();

        //调用请求之后统计时间
        return chain.filter(exchange).then().then(Mono.fromRunnable(() -> {
            Long endTime = System.currentTimeMillis();
            System.out.println("新请求: " + exchange.getRequest().getURI().getRawPath() + ", 耗时: " + (endTime - startTime) + "ms");
//            logger.info("新请求: " + exchange.getRequest().getURI().getRawPath() + ", 耗时: " + (endTime - startTime) + "ms");
        }));
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
